<?php

namespace App\Repositories\Impl;

use App\Models\User;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Repositories\Contracts\UserRepository;
use App\Repositories\Transformers\UserTransformer;


class UserRepositoryImpl implements UserRepository
{
    public function paginate($perPage, $filter)
    {
        $result = User::paginate($perPage);
        return $result;
    }

    public function find($id, $columns = ['*'])
    {
        return User::find($id, $columns);
    }

    public function findByFilter($filter, $columns = ['*'])
    {
        $user = User::where($filter)->first($columns);
        $data = fractal($user, new UserTransformer());

        

        $result['data'] = $data->toArray();
        $result['user'] = $user;
        return $result;

    }

    public function store($data)
    {
        $user = User::create($data);

        $data = fractal($user, new UserTransformer());
        $result['data'] = $data->toArray();
        $result['user'] = $user;

        return $result;

    }

    public function update($data, $id)
    {
        return User::whereId($id)->updateOrCreate($data);
    }

    public function updateByFilter($data, $where)
    {
        return User::where($where)->update($data);
    }

    public function delete($id)
    {
        return User::whereId($id)->delete();
    }
}
